<template>
	<m-layout :title="!compyid?'开设分公司':'修改公司信息'">
		<template v-if="!userinfo.isv||this.form.id">
			<m-block spa="24" gap="10 30">
				<view class="top">
					<view class="flex-between border-bottom ptb-24">
						<view class="fs28 fw-600">
							<text class="text-red">*</text>
							<text>公司名称</text>
						</view>
						<view class="flex-1 ta-right pl-20">
							<input v-model="form.titel" class="fs28" type="text" placeholder="请输入公司名称">
						</view>
					</view>
					<view class="flex-between border-bottom ptb-24">
						<view class="fs28 fw-600">
							<text class="text-red">*</text>
							<text>公司地址</text>
						</view>
						<view class="flex-1 ta-right pl-20">
							<input v-model="form.address" class="fs28" type="text" placeholder="请输入公司详细地址">
						</view>
					</view>
					<view class="flex-between border-bottom ptb-24">
						<view class="fs28 fw-600">
							<text class="text-red">*</text>
							<text>法人姓名</text>
						</view>
						<view class="flex-1 ta-right pl-20">
							<input v-model="form.truename" class="fs28" type="text" placeholder="请输入公司法人姓名">
						</view>
					</view>
					<view class="flex-between border-bottom ptb-24">
						<view class="fs28 fw-600">
							<text class="text-red">*</text>
							<text>法人身份证</text>
						</view>
						<view class="flex-1 ta-right pl-20">
							<input v-model="form.idcard" class="fs28" type="text" placeholder="请输入公司法人身份证">
						</view>
					</view>
					<view class="flex-between border-bottom ptb-24">
						<view class="fs28 fw-600">
							<text class="text-red">*</text>
							<text>公司社会信用代码</text>
						</view>
						<view class="flex-1 ta-right pl-20">
							<input v-model="form.compycode" class="fs28" type="text" placeholder="请输入公司社会信用代码">
						</view>
					</view>
				</view>
				<!-- <view class="flex-column mt-12">
					<text class="fs28 fw-600">公司简介</text>
					<view class="feedback_box mt-12">
						<view class="flex" style="align-items: flex-start;">
							<textarea style="margin-left: 5rpx;" v-model="form.content" @input="changeContent"
								placeholder-class="placeholder" placeholder="请简单描述您的公司" />
						</view>
						<view>已写<text style="color: #FA2B47;">{{contentLength}}</text>个字</view>
					</view>
				</view> -->
				<view class="flex-column mt-16">
					<text class="mb-8 fs28 fw-600">
						<text class="text-red">*</text>
						<text>公司营业执照</text>
					</text>
					<view class="">
						<uploadPhoto ref="uploadPhoto" :length=1 v-model="imgList" placeholder="添加照片(必填)"></uploadPhoto>
					</view>
				</view>
			</m-block>
			<view class="flex">
				<text class="flex-1 fs21 ml-28 text-red ta-center">说明：*号为必填选项</text>
			</view>
			<m-block gap="24" bgColor="unset">
				<m-button class="ptb-20" @click="submitBefore()">确定</m-button>
			</m-block>
		</template>
		<template v-else>
			<m-block spa="24" gap="120 30 40">
				<view class="flex-column ai-center ptb-20">
					<m-icon type="certified-yes" size="120"></m-icon>
					<text class="ta-center mt-24 fs25 text-dark">开设分公司申请已提交</text>
					<m-button size="small" class="ptb-20 mt-60" @click="Jump('/pages/mine/myCompyName/myCompyName')"
						type="hollow">查看我的分公司</m-button>
				</view>
			</m-block>
		</template>
		<!--  -->
		<m-dialog title="提示" :loading="btnLoading" :showCancel="true" :isShow="submitShow" @handleCancel="submitShow = false;btnLoading = false;
			" @handleConfirm="submitFn">
			<view class="ta-center ptb-48">

				是否确定提交？
			</view>
		</m-dialog>
	</m-layout>
</template>

<script>
	import { mapState, mapActions } from 'vuex'
	// import { hhrpageFromCompy } from '@/api/Users.js';
	import { addCompay, mycompy } from "@/api/Payment.js"
	export default {
		data() {
			return {
				compyid: 0,
				contentLength: 0,
				imgList: [],
				form: {
					id: 0,
					titel: '', //  公司名称
					picurl: '', // 
					content: '', // 公司描述
					address: '', // 分公司地址
					compycode: '', // 分公司邀请码
					truename: '', //  法人姓名
					idcard: '', // 法人身份证
				},
				submitShow: false,
				btnLoading: false
			}
		},
		computed: {
			...mapState({
				userinfo: state => state.user.userinfo,
				companyInfo: state => state.user.companyInfo
			}),
		},
		onLoad({ compyid }) {
			this.compyid = compyid || 0;
			if (compyid) {
				this.form.id = compyid;
				this.getMycompyInfoFn()
				this.form.titel = this.companyInfo.compyname;
				this.form.address = this.companyInfo.address;
				this.form.content = this.companyInfo.content;
				this.form.picurl = this.companyInfo.picurl;
				this.form.truename = this.companyInfo.truename;
				this.form.idcard = this.companyInfo.idcard;
				this.form.compycode = this.companyInfo.compycode;
				this.imgList = this.form.picurl.split(',')
			}
		},
		methods: {
			async getMycompyInfoFn() {
				const { success, data } = await mycompy({
					compyid: this.compyid,
				});
				this.form.titel = data.titel;
				this.form.address = data.address;
				this.form.content = data.content;
				this.form.picurl = data.picurl;
				this.form.truename = data.truename;
				this.form.idcard = data.idcard;
				this.form.compycode = data.compycode;
				this.imgList = this.form.picurl.split(',')
			},
			async submitBefore() {
				if (!this.form.titel || !this.form.address || !this.form.truename || !this.form.idcard || !this.imgList
					.length || !this.form.compycode)
					return this.Toast('请输入必填选项内容')
				this.submitShow = true;
			},
			async submitFn() {
				this.form.picurl = this.imgList.toString()
				let res = await addCompay(this.form)
				if (res.success) {
					this.submitShow = false;
					this.Jump(-1)
					this.Toast('提交成功')
				}
			},
			changeContent(e) {
				this.contentLength = e.detail.cursor;
				this.form.content = e.detail.value;
			},
		}
	}
</script>

<style lang="scss">
	.feedback_box {
		font-size: 28rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #999999;
		padding: 12rpx;
		border: 2rpx solid #d2d2d2;
		border-radius: 16rpx;

		.placeholder {
			font-size: 28rpx;
			font-family: PingFang SC-Regular, PingFang SC;
			font-weight: 400;
			color: #999999;
		}

		.feedback_img_no {
			background: #F6F6F6;
			border-radius: 24rpx;
			font-size: 28rpx;
			font-family: PingFang SC-Regular, PingFang SC;
			font-weight: 400;
			color: #999999;
			padding: 44rpx 0;
			margin-top: 22rpx;
		}
	}
</style>